package com.dididoctor.patient.Activity.Usercentre;

import android.content.Context;

import com.dididoctor.patient.Activity.ConstantValue;
import com.dididoctor.patient.Bean.Token;
import com.dididoctor.patient.Http.MyHttpResponseHandler;
import com.dididoctor.patient.MV.BasePresenter;
import com.dididoctor.patient.MV.BusinessClient;
import com.dididoctor.patient.MV.Response;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： EditPhonePresenter
 * @包名称： com.dididoctor.patient.Activity.Usercentre.Userinfo
 * @创建人： zhanjunjie
 * @创建时间：2016/4/14 18:55
 * @修改人：
 * @修改时间：2016/4/14 18:55
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class EditPhonePresenter extends BasePresenter {

    private EditPhoneView view;

    public EditPhonePresenter(Context context, EditPhoneView view) {
        super(context, view);
        this.view = view;
    }

    public void updatephone(String phone, String checkCode) {
        RequestParams params = new RequestParams();
        params.put("phone", phone);
        params.put("checkCode", checkCode);
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        BusinessClient.post(ConstantValue.phoneUpdate, params, new MyHttpResponseHandler() {

            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    view.updateSucced();
                } else {
                    view.showToastMessage(response.getMsg());
//                    view.updatefail();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.updatefail();
            }
        });
    }

    /**
     * @param phone 获取验证码
     */
    public void getInvitationCode(String phone) {
        RequestParams params = new RequestParams();
        params.put("phone", phone);
        BusinessClient.post(ConstantValue.intCode, params, new MyHttpResponseHandler(view) {

            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    //TODO  请不要再开了，上线容易忘记
//                    view.showToastMessage(response.getJson());
                    view.getInvitationCode();
                } else {
//                    view.getInvitationCodeFail();
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getInvitationCodeFail();
            }
        });
    }
}
